package com.hmall.order.web;

import cn.hutool.core.util.IdUtil;
import com.hmall.feign.client.ItemFeign;
import com.hmall.feign.client.UserFeign;
import com.hmall.feign.common.pojo.Address;
import com.hmall.feign.common.pojo.Item;
import com.hmall.feign.common.utils.ThreadLocalUtil;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.vo.OrderVo;
import com.hmall.order.service.IOrderService;
import com.hmall.order.service.OrderDetailService;
import com.hmall.order.service.OrderLogisticsService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {
   @Autowired
   ItemFeign itemFeign;
   @Autowired
   UserFeign userFeign;
   @Autowired
   private IOrderService orderService;
   @Autowired
   private OrderDetailService orderDetailService;
   @Autowired
   private OrderLogisticsService orderLogisticsService;


   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }

   @PostMapping
   @GlobalTransactional //开启分布式事务
   public Long createOrder(@RequestBody OrderVo orderVo){
      //创建一个order对象
      Order order = new Order();
      //1.填充id
      order.setId(IdUtil.getSnowflakeNextId());
      //2.调用远程feign接口查询出商品
      Item item = itemFeign.getById(orderVo.getItemId());
      //3.计算总价
      Long totalFee = item.getPrice()*orderVo.getNum();
      //4.封装数据
      //4.1.填充金额
      order.setTotalFee(totalFee);
      //4.2.填充用户id
      Long userId  = Long.valueOf( ThreadLocalUtil.get().toString());
      order.setUserId(userId);
      //4.3.填充支付方式
      order.setPaymentType(orderVo.getPaymentType());
      //4.4.填充订单状态(初始为未支付)
      order.setStatus(1);
      //4.5.填充订单创建时间
      order.setCreateTime(new Date());
      //4.6.填充订单更新时间
      order.setUpdateTime(new Date());
      //5.将订单写入数据库
      orderService.save(order);
      log.info("------------------订单创建成功------------------");

      //6.将商品信息、orderId信息封装到OrderDetail对象中
      //6.1.创建一个OrderDetail对象
      OrderDetail orderDetail = new OrderDetail();
      //6.2.填充数据
      //订单id
      orderDetail.setOrderId(order.getId());
      //商品id
      orderDetail.setItemId(item.getId());
      //商品数量
      orderDetail.setNum(orderVo.getNum());
      //商品标题
      orderDetail.setTitle(item.getName());
      //商品单价
      orderDetail.setPrice(item.getPrice());
      //商品规格
      orderDetail.setSpec(item.getSpec());
      //商品图片
      orderDetail.setImage(item.getImage());
      //创建时间
      orderDetail.setCreateTime(new Date());
      //修改时间
      orderDetail.setUpdateTime(new Date());
      //7.将orderDetail对象添加到数据库
      orderDetailService.save(orderDetail);
      log.info("------------------订单详情创建成功------------------");
      Address address= userFeign.findAddressById(orderVo.getAddressId());
      //8.填充OrderLogistics对象信息
      OrderLogistics orderLogistics = new OrderLogistics();
      //8.1.将地址信息填充到orderLogistics
      BeanUtils.copyProperties(address,orderLogistics);
      //8.2.完善缺失的属性
      //8.2.1.填充订单id
      orderLogistics.setOrderId(order.getId());
      //8.2.2.填充phone;(address中是mobile,copy不进去,单独填充)
      orderLogistics.setPhone(address.getMobile());
      //8.2.3.填充创建时间
      orderLogistics.setCreateTime(new Date());
      //8.2.3.填充更新时间
      orderLogistics.setUpdateTime(new Date());
      //9.写入tb_order_logistics表
      orderLogisticsService.save(orderLogistics);
      log.info("------------------订单后勤创建成功------------------");
      //10.调用item-service的更改库存接口
      itemFeign.updateStock(item.getId(),orderVo.getNum() * -1);
      log.info(order.getId()+"");
      return order.getId();
   }
}
